#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h> 
#include <SDL/SDL_ttf.h>
#include "SDL/SDL_mixer.h"

void graff (SDL_Surface *ecran);
void graf (SDL_Surface *ecran);
void UpdatePersoRight();
void UpdatePersoLeft();
void Draw (SDL_Surface* img,int x ,int y ,SDL_Surface* ecr);
int  p=0;
SDL_Event event;
SDL_Surface *ecran = NULL,
*back ,*back1=NULL, *persodr[1],*persoga[1],*balle[2]; 

int i=0,j,a,k,keyl,keyr,test=0,continuer=1,fondx=0,fondy=0,ppx=50,ppy=400,kourax=650,kouray=420;
//The sound that will be played
Mix_Music *music;
void score (SDL_Surface *ecran)
{
int j;
SDL_Surface *scoreee[22] ;
SDL_Rect positionscoreee;
positionscoreee.x = 0;
positionscoreee.y = 300;
scoreee[0]= IMG_Load("dude1.png");
scoreee[1]=IMG_Load("dude2.png");
scoreee[2]= IMG_Load("dude3.png");
scoreee[3]=IMG_Load("dude4.png");
scoreee[4]= IMG_Load("dude5.png");
scoreee[5]=IMG_Load("dude6.png");
scoreee[6]= IMG_Load("dude7.png");
scoreee[7]=IMG_Load("dude8.png");
scoreee[8]= IMG_Load("dude9.png");
scoreee[9]=IMG_Load("dude10.png");
scoreee[10]= IMG_Load("dude11.png");
scoreee[11]=IMG_Load("dude12.png");
scoreee[12]= IMG_Load("dude13.png");
scoreee[13]=IMG_Load("dude14.png");
scoreee[14]= IMG_Load("dude15.png");
scoreee[15]=IMG_Load("dude16.png");
scoreee[16]= IMG_Load("dude17.png");
scoreee[17]=IMG_Load("dude18.png");
scoreee[18]= IMG_Load("dude19.png");
scoreee[19]=IMG_Load("dude20.png");
scoreee[20]= IMG_Load("dude21.png");
scoreee[21]=IMG_Load("dude22.png");

for(j=0;j<22;j++)
{
SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 255, 255, 255));

SDL_BlitSurface(scoreee[j], NULL, ecran, &positionscoreee);
positionscoreee.x=positionscoreee.x+18;
SDL_Flip(ecran);
SDL_Delay(70);
}
//SDL_FreeSurface(score[22]);
}

//le main 


int main()
{
SDL_Color couleurNoire = {0, 0, 0},couleurBlanche = {255, 255, 255};
char score[2000] = "";
int p=0;
int scores=400;
SDL_Surface *texte=NULL ;
SDL_Rect positiontexte;
positiontexte.x = 300;
positiontexte.y = 270;

SDL_Init(SDL_INIT_VIDEO);

//Initialize SDL_mixer

Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 1024 );

TTF_Init();
   // SDL_WM_SetIcon(IMG_Load("sdl_icone.bmp"), NULL);

    ecran = SDL_SetVideoMode(1300, 800, 32, SDL_HWSURFACE |SDL_DOUBLEBUF);
    SDL_WM_SetCaption("BASKETBALL", NULL);
 SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 0, 0, 0));

 TTF_Font *police = NULL;
 police = TTF_OpenFont("AA.ttf", 30);
 sprintf(score, "score : %d", scores);
texte = TTF_RenderText_Shaded(police, score, couleurNoire ,couleurBlanche);
SDL_BlitSurface(texte, NULL, ecran, &positiontexte);
SDL_Flip(ecran);
SDL_FreeSurface(texte);
if(p<47)
{
p++;
}
else
{
p=0;
}
     back=IMG_Load("bg.jpg");



persodr[0] = IMG_Load("11.png");
persodr[1] = IMG_Load("22.png");
persoga[0] = IMG_Load("1.png");
persoga[1] = IMG_Load("2.png");


balle[0]=IMG_Load("balle1.png");
balle[1]=IMG_Load("balle2.png");
balle[2]=IMG_Load("balle3.png");

//play sound with cat image
music = Mix_LoadMUS("ambiance.mp3");
Mix_PlayMusic(music,1);

SDL_EnableKeyRepeat(100, 100);
 while (continuer)
    {

        SDL_PollEvent(&event);
        switch(event.type)
        {
            case SDL_QUIT:
                continuer = 0;
break;
 case SDL_KEYDOWN:
switch (event.key.keysym.sym)
            {
                case SDLK_RIGHT: /* Appui sur la touche entrer la variable entrer recoit 1 */
keyr=1;
test=0;

		break;
                  case SDLK_LEFT:
keyl=1;
test=1;

break;

                 
break;
        

       case SDLK_ESCAPE:
  continuer=0;
break;
        }


i=0;



if(a==1 && test==0)
{

kourax=-10;
kouray-=10;
if(k<1)
{
k++;
}
else
{
k=0;
}

//i=1;
}


if(a==1&& test==1)
{

kourax+=10;
kouray-=10;
if(k<1)
{
k++;
}
else
{
k=0;
}


i=1;



} 


if(keyr==1)
{



ppx=650;
kourax=640;

} 

else if(keyl==1)
{


ppx=0;
kourax=90;
kouray=410;
Draw(persoga[i], ppx, ppy, ecran);
SDL_Flip(ecran);
} 

case SDL_KEYUP:
switch (event.key.keysym.sym)
            {
                case SDLK_RIGHT: /* Appui sur la touche entrer la variable entrer recoit 1 */
keyr=0;



		break;
                  case SDLK_LEFT:
keyl=0;

      break;

                  case SDLK_a:
a=1;

      break;
        }
break;

 }

printf("%d\n",kouray);

if(test==0)
{
UpdatePersoRight();
}
if(test==1)
{
UpdatePersoLeft();

}
if(kouray<125 && test==0)
{
kouray+=10;
UpdatePersoRight();
}

if(kouray<125 && test==1)
{
kouray+=10;
UpdatePersoLeft();
}

i=0;
}



SDL_FreeSurface(back);


 return EXIT_SUCCESS;
}

void UpdatePersoLeft()
{
SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 0, 0, 0));
SDL_Delay(200);
if(p<47)
{
p++;
}
else
{
p=0;
}
Draw(back, fondx, fondy, ecran);
Draw(persoga[i], ppx, ppy, ecran);
Draw(balle[k], kourax, kouray, ecran);
SDL_Flip(ecran);

}

void UpdatePersoRight()
{
SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 0, 0, 0));
SDL_Delay(200);
if(p<47)
{
p++;
}
else
{
p=0;
}
Draw(back, fondx, fondy, ecran);
Draw(persodr[i], ppx, ppy, ecran);
Draw(balle[k], kourax, kouray, ecran);
SDL_Flip(ecran);

}

void graff (SDL_Surface *ecran)
{
int i,c=0;
SDL_Surface *graff[2] ;
SDL_Rect positiongraff;
positiongraff.x = 176;
positiongraff.y = 270;
graff[0]= IMG_Load("dude4.png");
graff[1]=IMG_Load("dude15.png");

while(c<=6)
{
for(i=0;i<2;i++)
{
SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 255, 255, 255));

SDL_BlitSurface(graff[i], NULL, ecran, &positiongraff);

SDL_Flip(ecran);
SDL_Delay(70);
}
c++;
}
}
void graf (SDL_Surface *ecran)
{
int i,c=0;
SDL_Surface *graf[2] ;
SDL_Rect positiongraf;
positiongraf.x = 176;
positiongraf.y = 270;
graf[0]= IMG_Load("dude4.png");
graf[1]=IMG_Load("dude15.png");

while(c<=1)
{
for(i=0;i<2;i++)
{
SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 255, 255, 255));

SDL_BlitSurface(graf[i], NULL, ecran, &positiongraf);

SDL_Flip(ecran);

}
c++;
}
//SDL_FreeSurface(graf[2]);
}


void Draw (SDL_Surface* img,int x ,int y ,SDL_Surface* ecr)
{
SDL_Rect pos;
pos.x=x;
pos.y=y;
SDL_BlitSurface(img,NULL,ecr,&pos);
}

